package com.pocketsweet.ui.fragments;

import java.util.ArrayList;
import java.util.List;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.lidroid.xutils.ViewUtils;
import com.pocketsweet.MLApplication;
import com.pocketsweet.MLContext;
import com.pocketsweet.R;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.Main;
import com.pocketsweet.ui.Splash;
import com.pocketsweet.ui.uilib.ActionItem;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onRightContainerListener;
import com.pocketsweet.ui.uilib.MyFragmentPagerAdapter;
import com.pocketsweet.ui.uilib.MyViewPager;
import com.pocketsweet.ui.uilib.TitlePopup;
import com.pocketsweet.ui.uilib.TitlePopup.OnItemOnClickListener;

public class HomeFragment extends Fragment implements View.OnClickListener,
        OnTouchListener {
    static Context mContext;
    private View rootView;
    private LinearLayout linNewOnline;
    private LinearLayout linRecomend;

    public static MyViewPager mPager;// 页卡内容
    private List<Fragment> listViews; // Tab页面列表
    public static int currIndex = 0;// 当前页卡编号
    private static TitlePopup titlePopup;
    private static int choosedSex = 0;// 0为全部；1为男；2为女
    private MLApplication mlApplication;


    // 双击
    private int count = 0;
    private int firClick = 0;
    private int secClick = 0;

    private RelativeLayout rlLoverTip;
    // private RelativeLayout rlLoverCallTip;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, null);
        ViewUtils.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getActivity();
        // 第一次进去的提示
        initTip();
        Main.getInstance().getHeader().init(HeaderStyle.TEXT_TITLE_IMG);

        if (UserService.getCurrentUser() == null) {
            Intent intent = new Intent(getActivity().getApplicationContext(),
                    Splash.class);
            PendingIntent restartIntent = PendingIntent.getActivity(
                    getActivity().getApplicationContext(), 0, intent,
                    Intent.FLAG_ACTIVITY_NEW_TASK);
            AlarmManager mgr = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
            mgr.set(AlarmManager.RTC, System.currentTimeMillis(),
                    restartIntent);
            mlApplication = MLApplication.getInstance();
            mlApplication.finishActivity();
        }
        if (UserService.getCurrentUser() == null) {
            System.exit(0);
            Intent intent = new Intent(getActivity(), Splash.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            // Intent k = getBaseContext().getPackageManager()
            // .getLaunchIntentForPackage(getBaseContext().getPackageName());
            // k.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            // startActivity(k);
        }
        if (currIndex == 0) {// 筛选
            if (UserService.getCurrentUser().getGender() == 1) {
                choosedSex = 2;
            } else {
                choosedSex = 1;
            }
            if (choosedSex == 0) {
                Main.getInstance().getHeader()
                        .setRightImg(R.drawable.icon_sex_all);
            } else if (choosedSex == 1) {
                Main.getInstance().getHeader()
                        .setRightImg(R.drawable.icon_sex_man);

            } else if (choosedSex == 2) {
                Main.getInstance().getHeader()
                        .setRightImg(R.drawable.icon_sex_female);
            }
            // Main.getInstance().getHeader()
            // .setRightImg(R.drawable.screen_normal);
            // } else {
            // Main.getInstance().getHeader()
            // .setRightImg(R.drawable.icon_love_call);
        }
        Main.getInstance().getHeader().InitcursorView();
        setHeaderFunction();
        setUpComponent();
        InitViewPager();
    }

    private void initTip() {
        rlLoverTip = (RelativeLayout) getActivity().findViewById(
                R.id.rlLoverTip);
//		rlLoverCallTip = (RelativeLayout) getActivity().findViewById(
//				R.id.rlLoverCallTip);
        if (MLContext.getLoverTip() == 0) {
            rlLoverTip.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    rlLoverTip.setVisibility(View.GONE);
                    MLContext.setLoverTip();
                }
            });
            rlLoverTip.setVisibility(View.VISIBLE);
        }

    }

    public static void setHeaderFunction() {
        final HeaderLayout header = Main.getInstance().getHeader();
        header.setRighttContainerClickListener(new onRightContainerListener() {
            @Override
            public void onClick() {
                // if (currIndex == 0) {// 筛选
                // 实例化标题栏弹窗
                // 实例化标题栏弹窗
                titlePopup = new TitlePopup(Main.getInstance(),
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                // 给标题栏弹窗添加子类
                titlePopup.addAction(new ActionItem(Main.getInstance(), "  全部",
                        R.drawable.icon_sex_all));
                titlePopup.addAction(new ActionItem(Main.getInstance(), "  男生",
                        R.drawable.icon_sex_man));
                titlePopup.addAction(new ActionItem(Main.getInstance(), "  女生",
                        R.drawable.icon_sex_female));
                titlePopup.setItemOnClickListener(new OnItemOnClickListener() {

                    @Override
                    public void onItemClick(ActionItem item, int position) {
                        switch (position) {
                            case 0:
                                choosedSex = 0;
                                break;
                            case 1:
                                choosedSex = 1;
                                break;
                            case 2:
                                choosedSex = 2;
                                break;
                            default:
                                break;
                        }
                        if (Main.choosedSex != choosedSex) {
                            Main.choosedSex = choosedSex;
                            Intent intent2 = new Intent();
                            intent2.setAction(Main.ACTION_CHOOSE_CHANGE);
                            Main.getInstance().sendBroadcast(intent2);
                        }

                    }
                });
                titlePopup.show(header.llRightContainer);
                // getIvRightImg());
                // Intent screenDailog = new Intent(mContext,
                // DialogScreen.class);
                // screenDailog.putExtra("choosedSex", Main.choosedSex);
                // screenDailog.putExtra("choosedCity", Main.choosedCity);
                // screenDailog.putExtra("choosedType", Main.choosedType);
                // mContext.startActivity(screenDailog);
                // } else if (currIndex == 1) {// 恋爱呼叫
                // Main.rlLoverInvited.setVisibility(View.VISIBLE);
                // AVAnalytics.onEvent(Main.getInstance(), "恋爱呼叫");
                // }
            }
        });
    }

    private void InitViewPager() {
        mPager = (MyViewPager) getActivity().findViewById(R.id.vPager);
        listViews = new ArrayList<Fragment>();
        listViews.add(new NewOnlineFragment());
        listViews.add(new RecommendFragment());
        mPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(),
                listViews));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new MyOnPageChangeListener1());

    }

    private void setUpComponent() {
        View linNewOnline = getActivity().findViewById(R.id.linNewOnline);
        View linRecomend = getActivity().findViewById(R.id.linRecomend);
        // linNewOnline.setOnClickListener(this);
        // linRecomend.setOnClickListener(this);
        linNewOnline.setOnTouchListener(this);
        linRecomend.setOnTouchListener(this);
    }

    /**
     * 页卡切换监听
     */
    public class MyOnPageChangeListener1 implements OnPageChangeListener {
        public void onPageSelected(int arg0) {
            switch (arg0) {
                case 0:
                    if (currIndex == 1) {
                        Main.getInstance().getHeader().slidleft();
                    }
                    if (choosedSex == 0) {
                        Main.getInstance().getHeader()
                                .setRightImg(R.drawable.icon_sex_all);
                    } else if (choosedSex == 1) {
                        Main.getInstance().getHeader()
                                .setRightImg(R.drawable.icon_sex_man);
                    } else if (choosedSex == 2) {
                        Main.getInstance().getHeader()
                                .setRightImg(R.drawable.icon_sex_female);
                    }
                    // Main.getInstance().getHeader()
                    // .setRightImg(R.drawable.screen_normal);
                    Main.getInstance().getHeader()
                            .setRightContentShow(View.VISIBLE);
                    break;
                case 1:
                    // if (MLContext.getLoverCallTip() == 0) {
                    // rlLoverCallTip.setOnClickListener(new OnClickListener() {
                    //
                    // @Override
                    // public void onClick(View arg0) {
                    // rlLoverCallTip.setVisibility(View.GONE);
                    // MLContext.setLoverCallTip();
                    // }
                    // });
                    // rlLoverCallTip.setVisibility(View.VISIBLE);
                    // }

                    if (currIndex == 0) {
                        Main.getInstance().getHeader().slidRight();
                    }
                    // Main.getInstance().getHeader()
                    // .setRightImg(R.drawable.icon_love_call);
                    Main.getInstance().getHeader()
                            .setRightContentShow(View.INVISIBLE);
                    // Main.clearRecommendRecord();
                    break;
            }
            currIndex = arg0;
        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        public void onPageScrollStateChanged(int arg0) {
        }
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.linNewOnline:
                mPager.setCurrentItem(0);
                if (choosedSex == 0) {
                    Main.getInstance().getHeader()
                            .setRightImg(R.drawable.icon_sex_all);
                } else if (choosedSex == 1) {
                    Main.getInstance().getHeader()
                            .setRightImg(R.drawable.icon_sex_man);
                } else if (choosedSex == 2) {
                    Main.getInstance().getHeader()
                            .setRightImg(R.drawable.icon_sex_female);
                }
                // Main.getInstance().getHeader()
                // .setRightImg(R.drawable.screen_normal);
                Main.getInstance().getHeader().setRightContentShow(View.VISIBLE);

                break;
            case R.id.linRecomend:
                mPager.setCurrentItem(1);
                // Main.getInstance().getHeader()
                // .setRightImg(R.drawable.icon_love_call);
                Main.getInstance().getHeader().setRightContentShow(View.INVISIBLE);
                break;

            default:
                break;
        }

    }

    private void clickLinNewOnline() {
        mPager.setCurrentItem(0);
        if (choosedSex == 0) {
            Main.getInstance().getHeader().setRightImg(R.drawable.icon_sex_all);
        } else if (choosedSex == 1) {
            Main.getInstance().getHeader().setRightImg(R.drawable.icon_sex_man);
        } else if (choosedSex == 2) {
            Main.getInstance().getHeader()
                    .setRightImg(R.drawable.icon_sex_female);
        }
        // Main.getInstance().getHeader()
        // .setRightImg(R.drawable.screen_normal);
        Main.getInstance().getHeader().setRightContentShow(View.VISIBLE);
    }

    private void clickLinRecomend() {
        mPager.setCurrentItem(1);
        // Main.getInstance().getHeader()
        // .setRightImg(R.drawable.icon_love_call);
        Main.getInstance().getHeader().setRightContentShow(View.INVISIBLE);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        if (MotionEvent.ACTION_DOWN == event.getAction()) {
            if ((int) System.currentTimeMillis() - firClick > 1000) {
                count = 0;
                firClick = 0;
                secClick = 0;
            }
            count++;
            if (count == 1) {
                firClick = (int) System.currentTimeMillis();
                if (v.getId() == R.id.linNewOnline) {
                    clickLinNewOnline();

                } else if (v.getId() == R.id.linRecomend) {
                    clickLinRecomend();
                }
            } else if (count == 2) {
                secClick = (int) System.currentTimeMillis();
                if (secClick - firClick < 1000) {
                    // 双击事件
                    if (currIndex == 0) {
                        NewOnlineFragment.dropDownListView.setSelection(0);
                    } else if (HomeFragment.currIndex == 1) {
                        RecommendFragment.articleListview.setSelection(0);
                    }
                } else {
                    if (v.getId() == R.id.linNewOnline) {
                        clickLinNewOnline();

                    } else if (v.getId() == R.id.linRecomend) {
                        clickLinRecomend();
                    }
                }
                count = 0;
                firClick = 0;
                secClick = 0;
            }
        }
        return true;

    }

}
